package com.liuceng.zen.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liuceng.zen.mapper.NewRptFrontCampaignDailyMapper;
import com.liuceng.zen.pojo.domain.NewRptFrontCampaignDaily;
import com.liuceng.zen.web.controller.CampaignController;

@Service
public class NewRptFrontCampaignDailyService extends BaseService<NewRptFrontCampaignDaily> {
  private final static Logger LOG = LoggerFactory.getLogger(CampaignController.class);
  @Autowired
  NewRptFrontCampaignDailyMapper newRptFrontCampaignDailyMapper;


  public Map<String, Object> findRptData(Map<String, Object> params) {
    return newRptFrontCampaignDailyMapper.findRptData(params);
  }

  public Map<String, Object> findTotalRptData(Map<String, Object> params) {
    Map<String, Object> result = newRptFrontCampaignDailyMapper.findRptData(params);

    try {
      if (result != null) {
        result.put("ctr",
            result == null || result.get("pv") == null
                || 0 == Integer.parseInt(result.get("pv").toString()) ? new BigDecimal(0)
                    : new BigDecimal(Integer.parseInt(result.get("pc").toString()) * 100)
                        .divide(new BigDecimal(result.get("pv").toString()), 3, RoundingMode.UP));
        result.put("cpc",
            result == null || result.get("pc") == null
                || 0 == Integer.parseInt(result.get("pc").toString()) ? new BigDecimal(0)
                    : new BigDecimal(Double.parseDouble(result.get("total_cost").toString()))
                        .divide(new BigDecimal(result.get("pc").toString()), 3, RoundingMode.UP));
        result.put("viewPerCost",
            result == null || result.get("pv") == null
                || 0 == Integer.parseInt(result.get("pv").toString()) ? new BigDecimal(0)
                    : new BigDecimal(Double.parseDouble(result.get("total_cost").toString()) * 1000)
                        .divide(new BigDecimal(result.get("pv").toString()), 3, RoundingMode.UP));
        return result;
      } else {
        Map<String, Object> results = new HashMap<String, Object>();
        results.put("total_cost", 0);
        results.put("pv", 0);
        results.put("pc", 0);
        results.put("ctr", 0);
        results.put("cpc", 0);
        results.put("viewPerCost", 0);
        return results;
      }
    } catch (NumberFormatException e) {
      // TODO Auto-generated catch block
      LOG.error(e.getMessage());
      return null;
    }

  }

}
